/**
 *----------------------------------
 *- Author:xuwenyu Date:
 *----------------------------------
 * 页名：flyio拦截器
 * 功能：[拦截所有请求，配置请求及响应]
 */
import Fly from 'flyio/dist/npm/wx';
import qs from 'qs/dist/qs'
// import store from '../../store'
// const appid = "wx53c4e88c81fa5063"
const appid = "wxafae830db67af075"


const fly = new Fly();

//业务域名
//域名后面禁止有 "/" 
export const baseurl = "https://print.net-frog.com"
// export const baseurl = "http://10.22.75.241:3000"

//配置请求
fly.config.appid = appid
fly.config.baseURL = baseurl; // 基地址
fly.config.parseJson = true; // 解码JSON   
// fly.config.headers={'Content-type':'application/x-www-form-urlencoded;'} // Content-type
fly.config.timeout = 30000; // 10秒超时

//请求拦截器
fly.interceptors.request.use((request) => {
	
	let  token = uni.getStorageSync('token')
	
	// 如果有token则带上token
	if (token) {
		//request.headers['authorized'] = token
		request.headers['authorization'] = token
	}
	// 如果是POST请求，则序列化参数
	// if (request.method === 'POST' || request.method === 'post') {
	// 	request.body = qs.stringify(request.body)
	// 	console.log(request.body)
	// }


	// 发送请求
	return request
})

//响应拦截器
fly.interceptors.response.use(
	(response) => {
		

		//只将请求结果的data字段返回
		console.log("响应拦截",response.data)
		if(response.status===404){
			uni.showToast({
				title:'未知的接口',
				icon:'none'
			})
		}
		if(response.status===500){
			uni.showToast({
				title:'服务端处理错误',
				icon:'none'
			})
		}
		return response
	},
	(err) => {
		
	}
)

//暴露fly
export default fly;
